﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.SmartHealthcare.Infrastructure;
using VOL.Team.TeamIservices.MaterialManagement.IServices;

namespace VOL.Team.TeamIservices.MaterialManagement.Services
{
    /// <summary>
    /// 物料管理-物料台账
    /// </summary>
    public class MaterialLedgersService: IMaterialLedgersService
    {
        /// <summary>
        /// 物料台账
        /// </summary>
        private readonly IBaseRepository<MaterialLedger> _materialledgerRepository;

        public MaterialLedgersService(IBaseRepository<MaterialLedger> materialledgerRepository)
        {
            _materialledgerRepository = materialledgerRepository;
        }
        /// <summary>
        /// 查询物料台账-xzc
        /// </summary>
        /// <returns></returns>
        public IQueryable<MaterialLedger> GetMaterialLedger(string? qecode)
        {
            try
            {
                var materialledgerList = _materialledgerRepository.GetAll();
                materialledgerList = materialledgerList.WhereIF(qecode != null, m => m.QRCode == qecode);
                return materialledgerList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 新增物料台账-xzc
        /// </summary>
        /// <param name="materialledger"></param>
        /// <returns></returns>
        public async Task<int> AddMaterialLedger(MaterialLedger materialledger)
        {
            try
            {
                var materialledgerList = await _materialledgerRepository.AddAsync(materialledger);
                return materialledgerList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 更新物料台账-xzc
        /// </summary>
        /// <param name="materialledger"></param>
        /// <returns></returns>
        public async Task<int> UpdateMaterialLedger(MaterialLedger materialledger)
        {
            try
            {
                var materialledgerList = await _materialledgerRepository.UpdateAsync(materialledger);
                return materialledgerList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除物料台账-xzc
        /// </summary>
        /// <param name="materialledger"></param>
        /// <returns></returns>
        public async Task<int> DeleteMaterialLedger(int id)
        {
            try
            {
                var materialledgerList = await _materialledgerRepository.DeleteAsync(id);
                return materialledgerList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 批量删除物料台账
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAnageMaterialLedger(string Id)
        {
            try
            {
                string[] pid = Id.Split(',');
                var a = 0;
                foreach(var item in pid)
                {
                    a =  await _materialledgerRepository.DeletteRangeAsync(m=>m.Id == Convert.ToInt32(item));
                }
                return a;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
